package qreasymarket.movil;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class HorarioPestana extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.horario_pestana);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menuprincipal, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.PromocionService) {
			startActivity(new Intent(HorarioPestana.this, MenuCarrusel.class));
			return (true);
		}

		return (super.onOptionsItemSelected(item));
	}

	public void buscar(View view) {

		final EditText txtHorario = (EditText) findViewById(R.id.horario_buscar);
		final TableLayout tlHorario = (TableLayout) findViewById(R.id.tlHorarios);

		String horario = txtHorario.getText().toString();

		RestClient cliente = new RestClient(
				"http://192.168.1.176:8080/QR-EasyMarket_Web/rest/horario_buscar");
		cliente.AddParam("idhorario", horario);

		try {
			cliente.Execute(RequestMethod.GET);
			String jsontext = cliente.getResponse();
			Log.i("HorarioPestana", jsontext);

			JSONArray entries = new JSONArray(jsontext);

			String x = "";
			tlHorario.removeAllViews();

			int i;

			for (i = 0; i < entries.length(); i++) {

				JSONObject post = entries.getJSONObject(i);
				System.out.println(post);
				x += "Horario:" + post.getString("idHorario") + " " + "Turno1:"
						+ post.getString("turno1") + " " + "Turno2:"
						+ post.getString("turno2") + " " + "Turno3:"
						+ post.getString("turno3") + " " + "Turno4:"
						+ post.getString("turno4") + "\n";
				TableRow tr = new TableRow(getBaseContext());

				TextView horarios = new TextView(getBaseContext());
				horarios.setText(post.getString("idHorario"));
				// horarios.setText(Color.BLACK);

				TextView n = new TextView(getBaseContext());
				n.setText(" | ");

				TextView turnos1 = new TextView(getBaseContext());
				turnos1.setText(post.getString("turno1"));
				// turnos1.setText(Color.BLACK);

				TextView m = new TextView(getBaseContext());
				m.setText(" | ");

				TextView turnos2 = new TextView(getBaseContext());
				turnos2.setText(post.getString("turno2"));
				// turnos2.setText(Color.BLACK);

				TextView o = new TextView(getBaseContext());
				o.setText(" | ");

				TextView valueTV = new TextView(getBaseContext());
				valueTV.setText(post.getString("turno3"));
				// valueTV.setTextColor(Color.BLACK);

				TextView p = new TextView(getBaseContext());
				p.setText(" | ");

				TextView value4 = new TextView(getBaseContext());
				value4.setText(post.getString("turno4"));
				// value4.setText(Color.BLUE);

				tr.addView(horarios);
				tr.addView(n);
				tr.addView(turnos1);
				tr.addView(m);
				tr.addView(turnos2);
				tr.addView(o);
				tr.addView(valueTV);
				tr.addView(p);
				tr.addView(value4);
				// tr.addView(n);
				tlHorario.addView(tr);

			}

			Log.i("HorarioBuscarActivity", x);
		} catch (Exception e) {
			Log.i("HorarioBuscarActivity", ":" + e.getMessage());
		}

	}

}
